// import { commitSession, getSession } from "~/session.server";
import type { Route } from "./+types/login";
import { redirect, replace } from "react-router";
import { serialize ,parse} from "cookie";
/* 
export const action = async (params: Route.ActionArgs) => {
  const { request } = params
  const session = await getSession(request.headers.get("Cookie"));
  const auth = session.get('auth')
  console.log('when logout, auth is', auth);

  session.unset('auth');
  const cookie = await commitSession(session)
  console.log('after logout, auth is', session.get('auth'));
  console.log('cookie is', cookie);

  return replace('/session-test', {
    headers: { 'Set-Cookie': cookie }
  })
} */
export const action = async (params: Route.ActionArgs) => {
  const { request } = params
  const cookies =  parse(request.headers.get("Cookie")??'');
  const auth = cookies.auth
  console.log('when logout, auth is', auth);

  const cookie = serialize('auth','',{
    maxAge: 0,
    path: '/',
    httpOnly: true,
    sameSite: 'lax',
  })
  console.log('after logout, auth is', auth);
  console.log('cookie is', cookie);

  return replace('/session-test', {
    headers: { 'Set-Cookie': cookie }
  })
}